package org.su.webadapter;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.regex.Pattern;

/**
 * Created by kidbei on 15/5/28.
 */
public class IPHelper {

    private static String innerHostIp = null;
    private static Pattern ipPattern = Pattern.compile("^([0-9]{1,3}\\.){3}[0-9]{1,3}$");
    private static Pattern privateIpPattern = Pattern.compile("(^127\\.0\\.0\\.1)|(^10(\\.[0-9]{1,3}){3}$)|(^172\\.1[6-9](\\.[0-9]{1,3}){2}$)|(^172\\.2[0-9](\\.[0-9]{1,3}){2}$)|(^172\\.3[0-1](\\.[0-9]{1,3}){2}$)|(^192\\.168(\\.[0-9]{1,3}){2}$)");

    public static String getInnerHostIp() {
        if (innerHostIp == null) {
                String ip = null;
                try {
                    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
                    while (interfaces.hasMoreElements()) {
                        NetworkInterface iface = interfaces.nextElement();
                        // filters out 127.0.0.1 and inactive if
                        if (iface.isLoopback() || !iface.isUp())
                            continue;

                        Enumeration<InetAddress> addresses = iface.getInetAddresses();
                        while (addresses.hasMoreElements()) {
                            InetAddress addr = addresses.nextElement();
                            String _tempIp = addr.getHostAddress();
                            // find private ip.
                            if (ipPattern.matcher(_tempIp).matches()
                                    && privateIpPattern.matcher( _tempIp).matches()) {
                                ip = _tempIp;
                                break;
                            }
                        }
                    }
                } catch (SocketException e) {
                    throw new RuntimeException(e);
                }
                innerHostIp = ip;
            }
        return innerHostIp;
    }

    /**
     * 字符串ip转为long类型
     * @param strIp
     * @return
     */
    public static long ipToLong(String strIp) {
        long[] ip = new long[4];
        //先找到IP地址字符串中.的位置
        int position1 = strIp.indexOf(".");
        int position2 = strIp.indexOf(".", position1 + 1);
        int position3 = strIp.indexOf(".", position2 + 1);
        //将每个.之间的字符串转换成整型
        ip[0] = Long.parseLong(strIp.substring(0, position1));
        ip[1] = Long.parseLong(strIp.substring(position1+1, position2));
        ip[2] = Long.parseLong(strIp.substring(position2+1, position3));
        ip[3] = Long.parseLong(strIp.substring(position3+1));
        return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3];
    }

    public static long ipToLong(String strIp,long port) {
        long[] ip = new long[4];
        //先找到IP地址字符串中.的位置
        int position1 = strIp.indexOf(".");
        int position2 = strIp.indexOf(".", position1 + 1);
        int position3 = strIp.indexOf(".", position2 + 1);
        //将每个.之间的字符串转换成整型
        ip[0] = Long.parseLong(strIp.substring(0, position1));
        ip[1] = Long.parseLong(strIp.substring(position1+1, position2));
        ip[2] = Long.parseLong(strIp.substring(position2+1, position3));
        ip[3] = Long.parseLong(strIp.substring(position3+1));
        return (port << 32) + (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3];
    }





    public static String longToIp(long longIp) {
        StringBuffer sb = new StringBuffer("");

        //直接右移24位
        sb.append(String.valueOf((longIp >>> 24)));
        sb.append(".");
        //将高8位置0，然后右移16位
        sb.append(String.valueOf((longIp & 0x00FFFFFF) >>> 16));
        sb.append(".");
        //将高16位置0，然后右移8位
        sb.append(String.valueOf((longIp & 0x0000FFFF) >>> 8));
        sb.append(".");
        //将高24位置0
        sb.append(String.valueOf((longIp & 0x000000FF)));

        return sb.toString();
    }



    public static String longToIp2(long longIp) {
        StringBuffer sb = new StringBuffer("");
        long port =longIp>>>32;
        long temp = (longIp & 0xffffFFFFl);
        sb.append(longToIp(temp));
        sb.append(" ");
        sb.append(String.valueOf(port));
        return sb.toString();
    }


    public static void main(String[] args) {
        System.out.println(longToIp2(ipToLong("199.254.254.254",254889)));
    }
}
